<?php

class AnalysisController extends Controller
{
    public function actionIndex() {
        $this->pageTitle = Yii::t('app', 'Manager step analysis');

        $model = new Analysis();

        $this->render('index', array(
            'model' => $model,
        ));
    }

    public function actionAdd() {
        $this->pageTitle = Yii::t('app', 'Manager step analysis');

        $model = new Analysis();

        if (isset($_POST['Analysis']) && $data = $_POST['Analysis']) {
            $model->attributes = $data;
            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Matter created success!'));
                $this->redirect(array('analysis/index'));
            }
        }

        $this->render('add', array('model' => $model));
    }

    public function actionUpdate($id = null) {
        $this->pageTitle = Yii::t('app', 'Manager step analysis');
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        //Get Analysis type
        $model = Analysis::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        if (isset($_POST['Analysis']) && $data = $_POST['Analysis']) {
            $model->attributes = $data;
            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Step analysis updated success!'));
                $this->redirect(array('analysis/index'));
            }
        }

        $this->render('update', array('model' => $model));
    }

    public function actionDelete($id = null) {
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        //Get Analysis by id
        $model = Analysis::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        $model->del_flg = 1;
        if ($model->save(false)) {
            if(!isset($_GET['ajax'])){
                Yii::app()->user->setFlash('success', Yii::t('app', 'Analysis - Deleted Successfully'));
                $this->redirect(array('analysis/index'));
            }
            else{
                echo '<div class="alert alert-success fade in"><button type="button" class="close" data-dismiss="alert">×</button>'.Yii::t('app', 'Analysis - Deleted Successfully').'</div>';
                die();
            }

        }

    }
}